




//
//  UIView+Animation.m
//  Animate
//
//  Created by pg on 16/5/6.
//  Copyright © 2016年 person. All rights reserved.
//

#import "UIView+Animation.h"
#import <objc/runtime.h>

#define LAYER_COLOR UIColorFromHex(0xF8290a)

@implementation UIView (Animation)

-(NSMutableArray *)layers{
    return objc_getAssociatedObject(self, "layers");
}

-(void)setLayers:(NSMutableArray *)layers{
    objc_setAssociatedObject(self, "layers", layers, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(CALayer *)createAnimationLayer{
    CALayer *layer = [CALayer layer];
    layer.bounds = self.bounds;
    layer.position = self.center;
    layer.backgroundColor = LAYER_COLOR.CGColor;
    layer.cornerRadius = layer.bounds.size.width/2;
    if (self.superview) {
        [self.superview.layer addSublayer:layer];
        [self.superview.layer insertSublayer:layer atIndex:0];
    }
    return layer;
}

-(void)scaleAnimation:(CALayer *)layer from:(NSNumber *)from to:(NSNumber *)to duration:(double)duration{
    NSString *keyPath = @"transform.scale";
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
    animation.duration = duration;
    animation.fromValue = from;
    animation.toValue = to;
    animation.removedOnCompletion = NO;
    animation.repeatCount = MAXFLOAT;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [layer addAnimation:animation forKey:@"scale"];
}
-(void)opacityAnimation:(CALayer *)layer from:(NSNumber *)from to:(NSNumber *)to duration:(double)duration{
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.fromValue = from;
    animation.toValue = to;
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    animation.repeatCount = MAXFLOAT;
    [layer addAnimation:animation forKey:@"opacity"];
}

-(void)animationScaleFrom:(CGFloat)scaleFrom to:(CGFloat)scaleTo
              opacityFrom:(CGFloat)opacityFrom to:(CGFloat)opacityTo
                durantion:(double)duration timeInvert:(CGFloat)invert{
    
    for (CALayer *layer in self.layers) {
        [layer removeFromSuperlayer];
    }
    [self layoutIfNeeded];
    int num = (int) duration/invert;
    if (!self.layers) {
        self.layers = [[NSMutableArray alloc]init];
    }
    for (int i = 0; i < num; i++) {
        CALayer *layer = [self createAnimationLayer];
        NSArray *array = @[ layer, @(scaleFrom), @(scaleTo), @(opacityFrom), @(opacityTo),@(duration)];
        [self performSelector:@selector(startAnimationWithLayer:) withObject:array afterDelay:invert*i];
        [self.layers addObject:layer];
    }
}

-(void)removeAnimate{
    for (CALayer *layer in self.layers) {
        [layer removeFromSuperlayer];
    }
    [self layoutIfNeeded];
}

-(void)startAnimationWithLayer:(NSArray *)info{

    [self scaleAnimation:info[0] from:info[1] to:info[2] duration:[info[5]doubleValue]];
    [self opacityAnimation:info[0] from:info[3] to:info[4] duration:[info[5]doubleValue]];
}
@end
